package HeSuanServer.service.Impl;

import HeSuanServer.POJO.Org;
import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.EmpSearch;
import HeSuanServer.POJO.Employee;
import HeSuanServer.mapper.EmpMapper;
import HeSuanServer.mapper.OrgMapper;
import HeSuanServer.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述:
 * @author: LH
 * @date: 2022/12/6
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private OrgMapper orgMapper;
    @Override
    public Result findAll() {
        log.info("开始查询");
        List<Employee> all;
        Result result=new Result();
        try {
            all = empMapper.findAll();
//            for (Employee employee : all) {
//                map.put(employee.getId(),employee);
//            }
            result.setData(all);
            result.setMsg("查询成功！");
            result.setStatusCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("查询失败！");
            result.setStatusCode(500);
        }finally {}
        log.info("结束查询");
        return result;
    }

    @Override
    public Result getById(Integer id) {
        log.info("根据id查询id："+id);
        Employee employee = null;
        Result result=new Result();
        if (employee==null) {
            try {
                employee = empMapper.getById(id);
                log.info("查询数据库id:"+id);
            } catch (Exception e) {
                result.setStatusCode(500);
                e.printStackTrace();
                return result;
            }
        }
        result.setData(employee);
        result.setStatusCode(200);
        return result;
    }

    @Override
    public Result editById(Employee emp) {
        log.info("开始编辑id:"+emp.getId());
        //删除缓存
        Result result=new Result();
//       根据机构名称查询机构id
        Integer id=orgMapper.getByName(emp.getOrgName());
        emp.setOrgId(id.toString());
        try {
            empMapper.editById(emp);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("添加失败");
            result.setStatusCode(500);
            return result;
        }
        result.setStatusCode(200);
        result.setMsg("修改成功");
        return result;
    }

    @Override
    public Result add(Employee emp) {
        log.info("开始添加");
        Result result=new Result();
        try{
            Org org = orgMapper.byId(Integer.parseInt(emp.getOrgId()));
            String orgName = org.getOrgName();
            emp.setOrgName(orgName);
            empMapper.add(emp);
            result.setStatusCode(200);
            result.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatusCode(500);
            result.setMsg("添加失败");
        }
        log.info("添加完成");
        return result;
    }

    @Override
    public Result del(Integer id) {
        Result R=new Result();
        log.info("开始删除:"+id);
        try {
            empMapper.del(id);
            R.setStatusCode(200);
            R.setMsg("删除成功:"+id);
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setMsg("添加失败:"+id);
        }
        log.info("删除完成:"+id);
        return R;
    }

    @Override
    public Result getByName(String name) {
        log.info("根据姓名查询开始---"+name);
        List<Employee> list=null;
        Result R=new Result();
        try {
            list = empMapper.getByName(name);
            R.setData(list);
            R.setStatusCode(200);
            R.setMsg("根据姓名查询成功");
        }catch (Exception e){
            e.printStackTrace();
            R.setData(list);
            R.setStatusCode(500);
            R.setMsg("根据姓名查询失败");
        }
        log.info("根据姓名查询结束");
        return R;
    }

    @Override
    public Result likeByName(String name) {
        log.info("根据姓名模糊查询开始---"+name);
        List<Employee> list=null;
        Result R=new Result();
        try {
            list=empMapper.likeByName(name);
            R.setMsg("模糊查询成功");
            R.setData(list);
            R.setStatusCode(200);
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setData(list);
            R.setMsg("模糊查询失败");
        }
        log.info("根据姓名查询结束");
        return R;
    }

    @Override
    public Result empSearch(EmpSearch search) {
        log.info("开始查询---"+search);
        Result R=new Result();
        List<Map> list=null;
        try {
            list=empMapper.Search(search);
            R.setMsg("查询成功!");
            R.setStatusCode(200);
            R.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            R.setStatusCode(500);
            R.setMsg("查询失败!");
        }
        log.info("结束查询");
        return R;
    }

    @Override
    public Result byPage(EmpSearch search) {
        Integer current = search.getCurrent();
        current=(current-1)*search.getSize();
        search.setCurrent(current);
        List<Map> list=null;
        Result R=new Result();
        Map<String,Object> map1=new HashMap();
        System.out.println(search);
        try {
            list=empMapper.Search(search);
            int count = empMapper.count(search);
            map1.put("list",list);
            map1.put("total",count);
            R.setMsg("分页查询成功!");
            R.setStatusCode(200);
        }catch (Exception e){
            e.printStackTrace();
            R.setMsg("分页查询失败!");
            R.setStatusCode(500);
        }
        R.setData(map1);
        return R;
    }
}
